/**
 * 
 */
package com.zhang.junit;


import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.zhang.pojo.User;


public class mybatisTest {
@Test
public void testName() throws Exception {
	String resource ="sqlMapConfig.xml";
	//加载核心配置文件
	InputStream in= Resources.getResourceAsStream(resource);
	//创建sqlsession工厂
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
	//创建sqlsession
	SqlSession sqlsession = sqlSessionFactory.openSession();
	User user=null;
	//执行sql
	try {

		user= sqlsession.selectOne("com.zhang.mapper.userdao.load",10);		
		
	} catch (Exception e) {
		// TODO: handle exception
		
	}finally{
		sqlsession.close();
	}
	System.out.println("name"+user);
 	
}

//根据用户名称模糊查询用户列表
@Test
public void testfindbyUserName() throws Exception {
	String resource ="sqlMapConfig.xml";
	//加载核心配置文件
	InputStream in= Resources.getResourceAsStream(resource);
	//创建sqlsession工厂
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
	//创建sqlsession
	SqlSession sqlsession = sqlSessionFactory.openSession();
	//执行sql
	try {

		List<User> user2=  sqlsession.selectList("com.zhang.mapper.userdao.findorlikeid", "王五");		
		for (User user : user2) {
			System.out.println(user2);
		}
		
	} catch (Exception e) {
		// TODO: handle exception
		
	}finally{
		sqlsession.close();
	}

 	
}
//添加用户
@Test
public void insertUsername() throws Exception {
	String resource ="sqlMapConfig.xml";
	//加载核心配置文件
	InputStream in= Resources.getResourceAsStream(resource);
	//创建sqlsession工厂
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
	//创建sqlsession
	SqlSession sqlsession = sqlSessionFactory.openSession();
	//执行sql
	User user=new User();
	user.setUsername("王撒");
	user.setBirthday("2017-01-02");
	user.setSex("男");
	user.setAddress("北京西城");
	try {
		  int insert = sqlsession.insert("com.zhang.mapper.userdao.insertUser",user);
		  sqlsession.commit();
		 
	} catch (Exception e) {
		// TODO: handle exception
		
	}finally{
		sqlsession.close();
	}

	 System.out.println(user.getId());
}
//修改用户
@Test
public void updateorUser() throws Exception {
	String resource ="sqlMapConfig.xml";
	//加载核心配置文件
	InputStream in= Resources.getResourceAsStream(resource);
	//创建sqlsession工厂
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
	//创建sqlsession
	SqlSession sqlsession = sqlSessionFactory.openSession();
	//执行sql
	User user=new User();
	user.setId(10);
	user.setUsername("王五无");
	user.setBirthday("2017-01-02");
	user.setSex("男");
	user.setAddress("北京西城");
	try {
		  int insert = sqlsession.update("com.zhang.mapper.userdao.updateoruser",user);
		  sqlsession.commit();
		 
	} catch (Exception e) {
		// TODO: handle exception
		
	}finally{
		sqlsession.close();
	}

	 System.out.println();
}
//删除用户
@Test
public void deleorUser() throws Exception {
	String resource ="sqlMapConfig.xml";
	//加载核心配置文件
	InputStream in= Resources.getResourceAsStream(resource);
	//创建sqlsession工厂
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
	//创建sqlsession
	SqlSession sqlsession = sqlSessionFactory.openSession();
	//执行sql
	try { 
		  sqlsession.delete("com.zhang.mapper.userdao.deleorUser",28);
		  sqlsession.commit();
		 
	} catch (Exception e) {
		// TODO: handle exception
		
	}finally{
		sqlsession.close();
	}

	 System.out.println();
}
}
